/*
 * @Author: nengwu 409791297@qq.com
 * @Date: 2020-06-25 10:20:03
 * @LastEditors: mylesshie@gmail.com
 * @LastEditTime: 2020-09-12 23:00:45
 * @description:
 */

import React, { useCallback, useEffect, useState } from 'react'
import { StatusBar, SafeAreaView, FlatList, View } from 'react-native'
import { WebView } from 'react-native-webview'
import HTML from 'react-native-render-html'
import { useSelector, useDispatch } from 'react-redux'
import PreItem from '@/components/PreItem'
import EmptyList from '@/components/EmptyList'
import Headers from '@/components/Headers'
import * as activityActions from '@/actions/activity'
import styles from './styles'

const Prediction = props => {
  const dispatch = useDispatch()
  const activityList = useSelector(state => state.activity.list)
  const pageEnd = useSelector(state => state.activity.listPageEnd)
  const [pageNum, setPageNum] = useState(1)

  const [webUrl, setWebUrl] = useState('')
  const [isRefreshing, setIsRefreshing] = useState(false)
  const pageSize = 20

  global.usertokenI = ""
  global.userIdI = ""



  const getList = useCallback(() => {
    dispatch(activityActions.getActivitys(pageNum, pageSize)).then(res => {
      setIsRefreshing(false)
    })
  }, [dispatch])

  useEffect(() => {
    getList()

  }, [pageNum, pageSize])

  useEffect(() => {
    // _goDetail()
  }, [])

  const _renderItem = ({ item }) => {
    return (
      <PreItem
        teamLeftTitle={item.teamName1}
        teamLeftIcon={item.teamIcon1}
        teamRightTitle={item.teamName2}
        teamRightIcon={item.teamIcon2}
        startTime={item.startTime}
        status={item.status}
        videoUrl={item.viedoUrl}
        title={item.title}
        scoreLeft={item.team1Score}
        scoreRight={item.team2Score}
      />
    )
  }



  const _onRefresh = () => {
    setPageNum(1)
    setIsRefreshing(true)
    getList()

  }


  const _goDetail = async () => {
    let loginData = {}
    try {
      loginData = await global.storage.load({ key: 'loginState' })
      // return
    } catch (err) {
      // loginData = {}
    }

    // console.log('22222222')

    // console.log(JSON.stringify(loginData.uid))
    // console.log(JSON.stringify(loginData.token))
    // // console.log(testId)
    // console.log('2222222')
    return loginData



  }



  // global.storage.load({ key: 'loginState' }).then(res => {
  //   if (res.token) {
  //     // console.log('12312312312312312312')
  //     // console.log(res.token);
  //     // console.log(res.uid);
  //     let data = {
  //       userToken: res.token,
  //       userId: res.uid,


  //     };
  //     global.usertokenI = res.token
  //     global.userIdI = res.uid

  //     // this.web && this.web.postMessage('dasdasdas');//发送消息到H5
  //     // this.web && this.web.postMessage(JSON.stringify(data));//发送消息到H5

  //     // dispatch(userActions.getUserInfo())
  //   }
  // })


  // const testId = encodeURIComponent(global.userIdI)
  _goDetail().then((data) => {
    console.log('1111111111')

    console.log(data.token)
    // console.log('==')
    // console.log(data.token)
    let tk = encodeURIComponent(data.token)
    // let tk = data.token
    setWebUrl('https://wap.sctynews.cn/h5/index.html/' + 'id=' + JSON.stringify(data.uid) + 'token=' + data.token)
    // setWebUrl('http://192.168.110.205:8080/' + 'id=' + JSON.stringify(data.uid) + 'token=' + data.token)
    // setWebUrl('https://wap.sctynews.cn/h5/index.html#/')
    // setWebUrl('http://192.168.110.205:8080/')
    console.log(webUrl)
    console.log('1111111111')
    console.log(StatusBar.currentHeight)
    StatusBar.setTranslucent(false);
    StatusBar.setBackgroundColor("#cf3037")
  })

  // const testId2 = "123231"

  return (
    <>

      <StatusBar
        barStyle="dark-content"
        backgroundColor="#cf3037"
        translucent={false}
      />

      <SafeAreaView style={styles.wrap}>
        <WebView

          // source={{ uri: 'http://139.155.248.30:8002/h5/index.html#/' }}

          source={{ uri: webUrl }}
          style={styles.webview}
          useWebKit={true}
          startInLoadingState={true}
          onMessage={this._onMessage}
          javaScriptEnabled={true}

          // onLoadEnd={_onLoadEnd}
          allowsInlineMediaPlayback={true}
        />
      </SafeAreaView>




    </>
  )
}

export default Prediction
